package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.MyMovement;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendServiceimpl implements FriendService {
    @Autowired
    private MongoTemplate mongoTemplate;
    //添加好友
    @Override
    public void addContacts(Long userId, Long friendId) {
        //判断两人是不是好友
        boolean exists = mongoTemplate.exists(Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);
        if (exists==false){
            Friend friend = new Friend();
            friend.setCreated(System.currentTimeMillis());
            friend.setUserId(userId);
            friend.setFriendId(friendId);
            //你加对方好友
            mongoTemplate.save(friend);
            //查询他的动态
            List<MyMovement> frientMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + friendId);
            if (CollectionUtil.isNotEmpty(frientMovementList)) {
                //将好友的动态添加道我的动态表中
                for (MyMovement friendmovement : frientMovementList) {
                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setUserId(friendId);
                    friendMovement.setPublishId(friendmovement.getPublishId());
                    friendMovement.setCreated(System.currentTimeMillis());
                    //将动态添加到好友动态中
                    mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + userId);

                }
            }
        }
         exists = mongoTemplate.exists(Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId)), Friend.class);
        if (exists==false) {
            //添加好友
            Friend friend = new Friend();
            friend.setUserId(friendId);
            friend.setFriendId(userId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
            //将我的动态添加到好友动态中
            List<MyMovement> myMovementList = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);
            //判断
            if (CollectionUtil.isNotEmpty(myMovementList)) {
                for (MyMovement myMovement : myMovementList) {
                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setUserId(userId);
                    friendMovement.setPublishId(myMovement.getPublishId());
                    friendMovement.setCreated(System.currentTimeMillis());
                    //添加
                    mongoTemplate.save(friendMovement,ConstantUtil.MOVEMENT_FRIEND+friendId);
                }
            }
        }


    }
  //查询好友列表
    @Override
    public PageBeanVo findContactsByPage(Long userId, Integer pageNum, Integer pageSize) {
        //创建条件
        Query query = Query.query(Criteria.where("userId").is(userId)).with(Sort.by(Sort.Order.desc("created"))).skip((pageNum - 1) * pageSize).limit(pageSize);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        Long total = mongoTemplate.count(query,Friend.class);
        return new PageBeanVo(pageNum,pageSize,total,friendList);
    }
}
